package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.ContractDao;
import cn.gson.crm.model.dao.MoneyBackPlanDao;
import cn.gson.crm.model.mapper.ContractMapper;
import cn.gson.crm.model.mapper.MoneyBackPlanMapper;
import cn.gson.crm.model.mapper.RefundRecordMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class MoneyBackPlanService {
    @Autowired
    MoneyBackPlanMapper moneyBackPlanMapper;
    @Autowired
    MoneyBackPlanDao moneyBackPlanDao;
    @Autowired
    RefundRecordMapper refundRecordMapper;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前登录人下所有回款计划
    public Map<String,Object> selectmoney(HttpSession session, Integer m, Integer page, Integer size){
        m=fouyuan(session);
        List<MoneyBackPlan> moneyBackPlans2=moneyBackPlanMapper.selectmoney(m);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<MoneyBackPlan> moneyBackPlans=moneyBackPlanMapper.selectmoney(m);
        //单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        String result="";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        //查询当前人下所有客户
        List<Customer>  customerList=moneyBackPlanMapper.selectcustomer(ygId);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",moneyBackPlans);
        map.put("customerList",customerList);
        map.put("number","HK"+newDate+result);
        map.put("tong",moneyBackPlans2);
        return map;

    };
    //根据客户id查询没有被退换并且状态是发货的的订单号
    public List<Order> selectorder(Integer cid){
        return moneyBackPlanMapper.selectorder(cid);
    };
    //根据时间查询回款计划
    public Map<String,Object> selecttiem(HttpSession session,Integer m, String statr, String endtime,Integer page, Integer size){
        m=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<MoneyBackPlan> moneyBackPlans=moneyBackPlanMapper.selecttiem(m,statr,endtime);
        //查询当前人下所有客户
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",moneyBackPlans);
        return map;
    };
    //回款计划多条件查询
    public Map<String,Object> selectwhere(HttpSession session,Integer m,String where,Integer page, Integer size){
        m=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<MoneyBackPlan> moneyBackPlans=moneyBackPlanMapper.selectwhere(m,where);
        //查询当前人下所有客户
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",moneyBackPlans);
        return map;
    };
    //新增回款计划
    public void savemoney(MoneyBackPlan moneyBackPlan){
        moneyBackPlanDao.save(moneyBackPlan);
    }
    //根据id查询订单详情
    public List<OrderDetails> selectoid(Integer oid){
        return moneyBackPlanMapper.selectoid(oid);
    };
    public int selecthui(Integer oid,Integer money,Integer ordermoney){
        int zon=0;
        List<MoneyBackPlan> moneyBackPlans=moneyBackPlanMapper.selecthui(oid);
        for(int i=0;i<moneyBackPlans.size();i++){
            zon+=moneyBackPlans.get(i).getBackMoney();
        }
        System.out.println(zon+"--------------111111111111111111----------------");
        zon+=money;
        if(zon==ordermoney){
            refundRecordMapper.updateorder(oid);
        }
        //判断此对应订单是否回款计划总金额大于了
        if(zon>ordermoney){
            zon=zon-money;
            zon=ordermoney-zon;
            System.out.println(zon+"--------------22222111111111111111111----------------");
            return zon;
        }else{
            return -1;
        }

    };

}
